package com.bw.module_home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arout.ARouterPath
import com.bw.lib_mvicore.ui.BaseMviFragment
import com.bw.module_home.adapter.HomePagerAdapter
import com.bw.module_home.databinding.HomeActivityHomeMainBinding
import com.bw.module_home.databinding.HomeFragmentHomeBinding
import com.bw.module_home.intent.SimpleTypeIntent
import com.bw.module_home.model.entity.SimpleTypeEntity
import com.bw.module_home.state.SimpleTypeState
import com.bw.module_home.viewmodel.SimpleTypeViewModel
import com.bw.module_home.widget.XiGuaTitleBar
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = ARouterPath.HOME_PATH)
class HomeFragment : BaseMviFragment() {

    private lateinit var binding: HomeFragmentHomeBinding
    private lateinit var simpleTypeViewModel: SimpleTypeViewModel

    override fun generateViewModel() {
        simpleTypeViewModel = ViewModelProvider(this)[SimpleTypeViewModel::class.java]
    }

    override fun initEvent() {
        binding.xgtbHomeTitlebar.setOnClickCallback(object : XiGuaTitleBar.TitleBarClickCallback{
            override fun onClick(index: Int) {
                showMsg("点击了$index")
            }
        })
        //金币数量
        binding.rpvHomeRedpacket.setTvNum(200)
        binding.rpvHomeRedpacket.setStateCallback {
            showMsg("上报金币")
        }
    }

    override fun lazyLoad() {
        loadSimpleTypeData()
        loadTitleBarData()
    }

    private fun loadTitleBarData() {
        val list = mutableListOf<String>()
        list += "新闻咨询1"
        list += "新闻咨询2"
        list += "新闻咨询3"
        binding.xgtbHomeTitlebar.setDataSource(list)
    }

    private fun loadSimpleTypeData() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                simpleTypeViewModel.intents.send(SimpleTypeIntent.GetSimpleType)
            }
        }
    }
    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = HomeFragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                simpleTypeViewModel.state.collect{
                    when(it){
                        is SimpleTypeState.Success -> {
                            generateTabs(it.list)
                        }
                        is SimpleTypeState.Failed -> {
                            showMsg(it.message)
                        }
                        is SimpleTypeState.Init -> {

                        }
                    }
                }
            }
        }
    }

    private fun generateTabs(list: MutableList<SimpleTypeEntity>) {
        list?.let {
            binding.vpHome.adapter = HomePagerAdapter(list, childFragmentManager)
            binding.tlHomeVideotype.setupWithViewPager(binding.vpHome)
        }
    }


}